﻿using SoccerEvaluator.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Web;

namespace SoccerEvaluator.WebSite.App_Code
{
    /// <summary>
    /// 
    /// </summary>
    [DataObjectAttribute]
    public static class SoccerEvaluatorDataSource
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>

        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public static List<TemplateInfo> ListGroups(int groupId)
        {
            SoccerEvaluatorModelContainer context = new SoccerEvaluatorModelContainer();

            List<Template> list = context.Templates.Where(c => c.GroupId == groupId).ToList();

            return ObjectFactory.ToTemplateInfoList(list);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="templateId"></param>
        /// <returns></returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public static List<SystemUserInfo> ListUserScores(int templateId)
        {

            SoccerEvaluatorModelContainer context = new SoccerEvaluatorModelContainer();

            TemplateInfo project = ObjectFactory.ToTemplateInfo(context.Templates.FirstOrDefault(c => c.Id == templateId));


            List<SystemUserInfo> results = (from user in context.SystemUsers
                                            where context.UserScores.Any(e => e.SystemUserId == user.Id)

                                            select new SystemUserInfo
                                                        {
                                                            UserId = user.Id,
                                                            Name = user.Name,
                                                            Username=user.UserName
                                                        }).ToList();



            Evaluator evaluator = new Evaluator(project.ExactScore, "96FF96", project.CatchScore, "B4B4FF");

            results.ForEach(delegate(SystemUserInfo x)
            {
                TemplateInfo userTemplate = new TemplateInfo();

                userTemplate.Scores = ObjectFactory.ToTemplateScoreInfoList(context.UserScores.Where(c => c.SystemUserId == x.UserId && c.TemplateId == templateId).ToList());

                x.Total = evaluator.Evaluate(project, userTemplate);
            });

            return results;

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="groupID"></param>
        /// <returns></returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public static List<SystemUserInfo> ListUserSummary(int groupID)
        {
            SoccerEvaluatorModelContainer context = new SoccerEvaluatorModelContainer();

            //Lista los templates que estan en modo calificacion para el grupo indicado
            List<Template> dbProjects = context.Templates.Where(c => c.GroupId == groupID & c.Mode == 0).ToList();
            
            //Resultado final de la consolidacion
            List<SystemUserInfo> finalData = new List<SystemUserInfo>();

            foreach (Template dbTemplate in dbProjects)
            {
                //Obtiene la lista de usuarios con sus puntajes
                List<SystemUserInfo> usersData = ListUserScores(dbTemplate.Id);

                //Consolida el resultado de los usuarios
                usersData.ForEach(delegate(SystemUserInfo y)
                {
                    SystemUserInfo finded = finalData.FindLast(delegate(SystemUserInfo x) { return x.UserId == y.UserId; });

                    if (finded == null)
                    {
                        finalData.Add(y);
                    }
                    else
                    {
                        finded.Total += y.Total;
                    }

                });
            }


            //Ordena la lista descendentemente por el total de puntos
            Comparison<SystemUserInfo> comparator = new Comparison<SystemUserInfo>(SystemUserInfo.CompareTotal);

            finalData.Sort(comparator);


            return finalData;
        }
    }
}